<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dbtest extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		//Loading database library.
		$this->load->database();
		
		//Selecting the title and body colums from table ci_entries (ci_ is the standard prefix loaded automaticly from configuration files) in database.
			//Alternative 1:
			$this->db->select('title, body');
			$this->db->from('entries');
			$query = $this->db->get(); //Fetch data by SQL-query: SELECT title, body FROM ci_entries
			
			//Alternative 2:
		//	$this->db->select('title, body');
		//	$query = $this->db->get('entries'); //Fetch data by SQL-query: SELECT title, body FROM ci_entries
			
			//Alternative 3 (NOT recomended):
		//	$query = $this->db->query('SELECT title, body FROM ci_entries'); //Fetch data by SQL-query: SELECT title, body FROM ci_entries
		
		//Printing data.
			//Alternative 1:
		//	if ($query->num_rows > 0) //Checking if there are any entries in the selected table.
		//		foreach ($query->result() as $row) //Going through all table rows fetched.
		//		{
		//			//Printing data.
		//			echo "<p>Title: " . $row->title . "<br />";
		//			echo "Body: " . $row->body . "</p>";
		//		}
		//	else
		//		echo "There is no entries in selected table!"; //Printing warning about no entries in selected table.
			
			//Alternative 2:
			if ($query->num_rows > 0) //Checking if there are any entries in the selected table.
			{
				foreach ($query->result_array() as $row) //Going through all table rows fetched.
				{
					//Printing data.
					echo "<p>Title: " . $row['title'] . "<br />";
					echo "Body: " . $row['body'] . "</p>";
				}
			}
			else
				echo "There is no entries in selected table!"; //Printing warning about no entries in selected table.
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */